home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / boot / noclick_.lha / NoClick / NoClick.c < prev    next >
C/C++ Source or Header  |  1995-09-15  |  13KB  |  605 lines

  1. ; /*
  2. dcc NoClick.c -r -mS -mi -proto -v -oNoClick
  3. Quit
  4. */
  5.  
  6. /*
  7. **
  8. **  $VER: NoClick.c 1.2 (15.9.95)
  9. **  NoClick 1.0
  10. **
  11. **  (C) Copyright 1994-95 by Roland 'Gizzy' Mainz
  12. **
  13. **
  14. */
  15.  
  16. /* amiga includes */
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <dos/dos.h>
  20. #include <dos/dosextens.h>
  21. #include <workbench/startup.h>
  22. #include <workbench/icon.h>
  23. #include <devices/trackdisk.h>
  24.  
  25. /* amiga prototypes */
  26. #include <clib/exec_protos.h>
  27. #include <clib/dos_protos.h>
  28. #include <clib/intuition_protos.h>
  29. #include <clib/icon_protos.h>
  30. #include <clib/alib_protos.h>
  31.  
  32. /* ansi includes */
  33. #include <string.h>
  34.  
  35. /* version string */
  36. #include "NoClick_rev.h"
  37.  
  38. /* misc defines */
  39. #define NAME "NoClick"
  40.  
  41. /* prototypes */
  42.                 void                  chkabort( void );
  43.                 long                  wbmain( struct WBStartup * );
  44.                 long                  main( long, STRPTR * );
  45.         static  void                  DefaultSettings( void );
  46.         static  void                  FreeInitProjectResult( void );
  47.  
  48.         static  void                  ClearRDA( void );
  49.         static  void                  ScanRDA( void );
  50.         static  void                  ScanToolTypes( STRPTR * );
  51.  
  52.         static  void                  RunTool( void );
  53.  
  54.         static  BOOL                  OpenLibStuff( void );
  55.         static  void                  CloseLibStuff( void );
  56.  
  57.         static  BOOL                  CreateBasicResources( void );
  58.         static  void                  DeleteBasicResources( void );
  59.  
  60.                 void                  AttemptOpenLibrary( struct Library **, STRPTR, STRPTR, ULONG );
  61.  
  62.                 STRPTR                StringSave( STRPTR );
  63.                 void                  FreeString( STRPTR );
  64.  
  65.                 APTR                  AllocVecPooled( APTR, ULONG );
  66.                 void                  FreeVecPooled( APTR, APTR );
  67.  
  68.  
  69. /* version string */
  70. STRPTR versionstring = VERSTAG;
  71.  
  72. long main_retval,
  73.      main_retval2;
  74.  
  75. static struct RDArgs  *startuprda;
  76.  
  77. /* shared libraries */
  78. struct Library *IntuitionBase,
  79.                *IconBase;
  80.  
  81. /* mempool for strings */
  82. APTR StringPool;
  83.  
  84. /* template for ReadArgs() */
  85. #define STARTUP_TEMPLATE "DEV=DEVICE,UNIT/N,NOCLICK=OFF/S,CLICK=ON/S"
  86.  
  87. static
  88. struct
  89. {
  90.     STRPTR  devicename;
  91.     long   *unit;
  92.     long   *noclick;
  93.     long   *click;
  94. } result;
  95.  
  96. static
  97. struct
  98. {
  99.     STRPTR  devicename;
  100.     ULONG   unit;
  101.     BOOL    noclick;
  102.     BOOL    click;
  103. } project;
  104.  
  105.  
  106. /* disable CTRL_C break support (DICE CTRL_C abort function) */
  107. void chkabort( void )
  108. {
  109. }
  110.  
  111.  
  112. long wbmain( struct WBStartup *wbstartup )
  113. {
  114.     return( main( 0L, (STRPTR *)wbstartup ) );
  115. }
  116.  
  117.  
  118. long main( long ac, STRPTR *av )
  119. {
  120.     LONG               numArgs,
  121.                        x;
  122.     struct WBStartup  *wbstartup;
  123.     struct WBArg      *wbarg;
  124.     struct DiskObject *tooldobj,
  125.                       *projectdobj;
  126.     BPTR               oldToolLock,
  127.                        oldProjectLock;
  128.  
  129.     x = main_retval2 = 0L;
  130.     main_retval = RETURN_OK;
  131.  
  132.     DefaultSettings();
  133.  
  134. /* Workbench */
  135.     if( ac == 0L )
  136.     {
  137.       wbstartup = (struct WBStartup *)av;
  138.  
  139.       if( CreateBasicResources() )
  140.       {
  141.         numArgs = wbstartup -> sm_NumArgs;
  142.         wbarg   = wbstartup -> sm_ArgList;
  143.  
  144.         if( *(wbarg[ 0 ] . wa_Name) )
  145.         {
  146.           if( wbarg[ 0 ] . wa_Lock )
  147.           {
  148.             oldToolLock = CurrentDir( (wbarg[ 0 ] . wa_Lock) );
  149.           }
  150.  
  151.           if( tooldobj = GetDiskObjectNew( (wbarg[ 0 ] . wa_Name) ) )
  152.           {
  153.             /* two possible cases when started from workbench ... */
  154.             if( numArgs < 2L )
  155.             {
  156.               /* ... first case, only our tool icon is given, create one project here */
  157.  
  158.               ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );
  159.  
  160.               RunTool();
  161.  
  162.               FreeInitProjectResult();
  163.             }
  164.             else
  165.             {
  166.               /* ... second case, a couple of project icons are given, multiple projects will start from here */
  167.               for( x = 1L ; x < numArgs ; x++ )
  168.               {
  169.                 if( wbarg[ x ] . wa_Lock )
  170.                 {
  171.                   oldProjectLock = CurrentDir( (wbarg[ x ] . wa_Lock) );
  172.                 }
  173.  
  174.                 if( *(wbarg[ x ] . wa_Name) )
  175.                 {
  176.                   if( projectdobj = GetDiskObject( (wbarg[ x ] . wa_Name) ) )
  177.                   {
  178.                     ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );
  179.                     ScanToolTypes( (STRPTR *)(projectdobj -> do_ToolTypes) );
  180.  
  181.                     RunTool();
  182.  
  183.                     FreeInitProjectResult();
  184.                     DefaultSettings();
  185.  
  186.                     FreeDiskObject( projectdobj );
  187.                   }
  188.                 }
  189.  
  190.                 if( wbarg[ x ] . wa_Lock )
  191.                 {
  192.                   CurrentDir( oldProjectLock );
  193.                 }
  194.               }
  195.             }
  196.  
  197.             FreeDiskObject( tooldobj );
  198.           }
  199.  
  200.           if( wbarg[ 0 ] . wa_Lock )
  201.           {
  202.             CurrentDir( oldToolLock );
  203.           }
  204.         }
  205.  
  206.         DeleteBasicResources();
  207.       }
  208.     }
  209.     else
  210.     {
  211. /* CLI/Shell */
  212.       if( CreateBasicResources() )
  213.       {
  214.         if( startuprda = ReadArgs( STARTUP_TEMPLATE, (LONG *)(&result), NULL ) )
  215.         {
  216.           /* did we get a CTRL_C signal ? */
  217.           if( !CheckSignal( SIGBREAKF_CTRL_C ) )
  218.           {
  219.             ScanRDA();
  220.  
  221.             RunTool();
  222.  
  223.             FreeInitProjectResult();
  224.           }
  225.           else
  226.           {
  227.             main_retval2 = ERROR_BREAK;
  228.             main_retval  = RETURN_WARN;
  229.           }
  230.  
  231.           FreeArgs( startuprda );
  232.         }
  233.         else
  234.         {
  235.           main_retval2 = IoErr();
  236.           main_retval  = RETURN_ERROR;
  237.         }
  238.  
  239.         PrintFault( main_retval2, NAME );
  240.  
  241.         DeleteBasicResources();
  242.       }
  243.     }
  244.  
  245.     SetIoErr( main_retval2 );
  246.  
  247.     return( main_retval );
  248. }
  249.  
  250.  
  251. void DefaultSettings( void )
  252. {
  253.     ClearRDA();
  254.  
  255.     result . devicename = TD_NAME;
  256.  
  257.     memset( (&project), 0, sizeof(project) );
  258. }
  259.  
  260.  
  261. void ClearRDA( void )
  262. {
  263.     memset( (&result), 0, sizeof(result) );
  264. }
  265.  
  266.  
  267. void ScanRDA( void )
  268. {
  269.     if( result . devicename )
  270.     {
  271.       FreeString( (project . devicename) );
  272.       project . devicename = StringSave( (result . devicename) );
  273.     }
  274.  
  275.     if( result . unit )
  276.     {
  277.       project . unit = (ULONG)(*(result . unit));
  278.     }
  279.  
  280.     if( result . click )
  281.     {
  282.       project . click = TRUE;
  283.     }
  284.  
  285.     if( result . noclick )
  286.     {
  287.       project . noclick = TRUE;
  288.     }
  289.  
  290.     ClearRDA();
  291. }
  292.  
  293.  
  294. void ScanToolTypes( STRPTR *tt )
  295. {
  296.     STRPTR s;
  297.  
  298.     if( s = FindToolType( tt, "DEVICENAME" ) )
  299.       result . devicename = s;
  300.  
  301.     if( s = FindToolType( tt, "DEV" ) )
  302.       result . devicename = s;
  303.  
  304.     if( s = FindToolType( tt, "UNIT" ) )
  305.       StrToLong( s, (LONG *)(&(project . unit)) );
  306.  
  307.     if( s = FindToolType( tt, "NOCLICK" ) )
  308.       result . noclick = (long *)s;
  309.  
  310.     if( s = FindToolType( tt, "OFF" ) )
  311.       result . noclick = (long *)s;
  312.  
  313.     if( s = FindToolType( tt, "CLICK" ) )
  314.       result . click = (long *)s;
  315.  
  316.     if( s = FindToolType( tt, "ON" ) )
  317.       result . click = (long *)s;
  318.  
  319.     ScanRDA();
  320. }
  321.  
  322.  
  323. static
  324. void FreeInitProjectResult( void )
  325. {
  326.     FreeString( (project . devicename) );
  327. }
  328.  
  329.  
  330. BOOL CreateBasicResources( void )
  331. {
  332.     if( OpenLibStuff() )
  333.     {
  334.       if( StringPool = CreatePool( MEMF_PUBLIC, 1024L, 1024L ) )
  335.       {
  336.         return( TRUE );
  337.  
  338.         /* DeletePool( StringPool ); */
  339.       }
  340.  
  341.       CloseLibStuff();
  342.     }
  343.  
  344.     return( FALSE );
  345. }
  346.  
  347.  
  348. void DeleteBasicResources( void )
  349. {
  350.     DeletePool( StringPool );
  351.     CloseLibStuff();
  352. }
  353.  
  354.  
  355. BOOL OpenLibStuff( void )
  356. {
  357.     if( IntuitionBase = OpenLibrary( "intuition.library", 39UL ) )
  358.     {
  359.       AttemptOpenLibrary( (&IconBase), NAME, ICONNAME, 37UL );
  360.  
  361.       if( IconBase )
  362.       {
  363.         return( TRUE );
  364.       }
  365.     }
  366.  
  367.     CloseLibStuff();
  368.  
  369.     return( FALSE );
  370. }
  371.  
  372.  
  373. void CloseLibStuff( void )
  374. {
  375.     CloseLibrary( IconBase );
  376.     CloseLibrary( IntuitionBase );
  377. }
  378.  
  379.  
  380. void AttemptOpenLibrary( struct Library **library, STRPTR title, STRPTR libname, ULONG libversion )
  381. {
  382.     struct EasyStruct LibNotFoundES =
  383.     {
  384.       sizeof(struct EasyStruct),
  385.       0,
  386.       title,
  387.       "%s\nnot found.",
  388.       "Retry|Cancel"
  389.     };
  390.  
  391.     struct EasyStruct LibWrongVersionES =
  392.     {
  393.       sizeof(struct EasyStruct),
  394.       0,
  395.       title,
  396.       "Requires at least\n%s version %lu",
  397.       "Cancel"
  398.     };
  399.  
  400.     if( (*library) == NULL )
  401.     {
  402.       for(;;)
  403.       {
  404.         /* attemp to open shared library */
  405.         (*library) = OpenLibrary( libname, 0UL );
  406.  
  407.         if( *library )
  408.         {
  409.           if( ((*library) -> lib_Version) < libversion )
  410.           {
  411.             (void)EasyRequest( NULL, (&LibWrongVersionES), 0UL, libname, libversion );
  412.  
  413.             CloseLibrary( (*library) );
  414.             (*library) = NULL;
  415.           }
  416.  
  417.           break;
  418.         }
  419.  
  420.         /* prompt the user */
  421.         if( EasyRequest( NULL, (&LibNotFoundES), 0UL, libname ) == 0L )
  422.         {
  423.           /* user canceled */
  424.           break;
  425.         }
  426.       }
  427.     }
  428. }
  429.  
  430.  
  431. STRPTR StringSave( STRPTR s )
  432. {
  433.     STRPTR saved;
  434.  
  435.     if( s )
  436.     {
  437.       if( saved = (STRPTR)AllocVecPooled( StringPool, (strlen( s ) + 4L) ) )
  438.       {
  439.         strcpy( saved, s );
  440.  
  441.         return( saved );
  442.       }
  443.     }
  444.  
  445.     return( NULL );
  446. }
  447.  
  448.  
  449. void FreeString( STRPTR s )
  450. {
  451.     if( s )
  452.     {
  453.       FreeVecPooled( StringPool, s );
  454.     }
  455. }
  456.  
  457.  
  458. APTR AllocVecPooled( APTR poolheader, ULONG memsize )
  459. {
  460.     ULONG *memory;
  461.  
  462.     memsize += sizeof(ULONG);
  463.  
  464.     if( memory = (ULONG *)AllocPooled( poolheader, memsize ) )
  465.     {
  466.       *memory = memsize;
  467.  
  468.       memory++;
  469.     }
  470.  
  471.     return( (APTR)memory );
  472. }
  473.  
  474.  
  475. void FreeVecPooled( APTR poolheader, APTR mem )
  476. {
  477.     ULONG *memory;
  478.  
  479.     if( mem )
  480.     {
  481.       memory = (ULONG *)mem;
  482.  
  483.       memory--;
  484.  
  485.       FreePooled( poolheader, memory, (*memory) );
  486.     }
  487. }
  488.  
  489.  
  490. void RunTool( void )
  491. {
  492.     struct IOExtTD        *diskreq;
  493.     struct MsgPort        *diskport;
  494.     struct TDU_PublicUnit *diskunit;
  495.     
  496.     diskunit = NULL; /* Used to detect if OpenDevice() fails */
  497.  
  498.     struct EasyStruct CantOpenDeviceES =
  499.     {
  500.       sizeof(struct EasyStruct),
  501.       0,
  502.       NAME,
  503.       "Cant open %s\n"
  504.       "unit %lu.",
  505.       "Retry|Cancel"
  506.     };
  507.  
  508.     struct EasyStruct ClickInfoES =
  509.     {
  510.       sizeof(struct EasyStruct),
  511.       0,
  512.       NAME,
  513.       "%s\n"
  514.       "unit %lu\n"
  515.       "has noclick flag %sset.",
  516.       "Ok"
  517.     };
  518.  
  519.     if( diskport = CreateMsgPort() )
  520.     {
  521.       if( diskreq = (struct IOExtTD *)CreateExtIO( diskport, (ULONG)sizeof(struct IOExtTD) ) )
  522.       {
  523.         for( ;; )
  524.         {
  525.           if( OpenDevice( (project . devicename), (project . unit), (struct IORequest *)diskreq, TDF_ALLOW_NON_3_5 ) == (BYTE)0 )
  526.           {
  527.             diskunit = (struct TDU_PublicUnit *)(diskreq -> iotd_Req . io_Unit);
  528.  
  529.             if( (project . click) || (project . noclick) )
  530.             {
  531.               Forbid();
  532.  
  533.                 if( project . click )
  534.                 {
  535.                   diskunit -> tdu_PubFlags ^= TDPF_NOCLICK;
  536.                 }
  537.  
  538.                 if( project . noclick )
  539.                 {
  540.                   diskunit -> tdu_PubFlags |= TDPF_NOCLICK;
  541.                 }
  542.  
  543.               Permit();
  544.             }
  545.             else
  546.             {
  547.               STRPTR ClickSet;
  548.  
  549.               Forbid();
  550.  
  551.                 ClickSet = ((((diskunit -> tdu_PubFlags) & TDPF_NOCLICK))?(""):("not "));
  552.  
  553.               Permit();
  554.  
  555.               if( Cli() )
  556.               {
  557.                 Printf( "%s unit %lu has noclick flag %sset.\n", (project . devicename), (project . unit), ClickSet );
  558.               }
  559.               else
  560.               {
  561.                 EasyRequest( NULL, (&ClickInfoES), NULL, (project . devicename), (project . unit), ClickSet );
  562.               }
  563.             }
  564.  
  565.             CloseDevice( (struct IORequest *)diskreq );
  566.  
  567.             break;
  568.           }
  569.           else
  570.           {
  571.             if( EasyRequest( NULL, (&CantOpenDeviceES), NULL, (project . devicename), (project . unit) ) == 0L )
  572.             {
  573.               /* user canceled */
  574.               break;
  575.             }
  576.           }
  577.         }
  578.  
  579.         if( diskunit == NULL )
  580.         {
  581.           main_retval2 = ERROR_OBJECT_NOT_FOUND;
  582.           main_retval  = RETURN_ERROR;
  583.         }
  584.  
  585.         DeleteExtIO( (struct IORequest *)diskreq );
  586.       }
  587.       else
  588.       {
  589.         /* Can't create IOExtTD */
  590.         main_retval2 = ERROR_NO_FREE_STORE;
  591.         main_retval  = RETURN_FAIL;
  592.       }
  593.  
  594.       DeleteMsgPort( diskport );
  595.     }
  596.     else
  597.     {
  598.       /* Can't create msgport */
  599.       main_retval2 = ERROR_NO_FREE_STORE;
  600.       main_retval  = RETURN_FAIL;
  601.     }
  602. }
  603.  
  604.  
  605.